package com.globefrog.framework.log;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;

/**
 * 创建自定义格式转换符有两步.
 * 1.继承ClassicConverter类。ClassicConverter对象负责从ILoggingEvent 提取信息，并产生一个字符串。
 *   例如，对日志中手机号、密码、银行答应的字段需要进行脱敏打印
 * 2.可对布局设置转换器,或者直接将声明成转换转换规则
 *
 *
 * @author caijinbang
 * @date 2020-11-08 15:19
 */
public class FilterMessageConverter extends ClassicConverter {

  public FilterMessageConverter() {
  }

  @Override
  public String convert(ILoggingEvent event) {
    String message = event.getFormattedMessage();
    return filter(event.getLoggerName(), message);
  }

  private String filter(String loggerName, String message) {
    LogMessageFilter filter = LogSettings.getInstance().getLogMessageFilter();
    return filter != null ? filter.filter(loggerName, message) : message;
  }
}
